package com.hbnu.system.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hbnu.system.config.BaseException;
import com.hbnu.system.constant.RoleConstant;
import com.hbnu.system.constant.StatusCheckConstant;
import com.hbnu.system.core.base.CommonPage;
import com.hbnu.system.mapper.UserMapper;
import com.hbnu.system.model.Enum.RoleEnum;
import com.hbnu.system.model.dto.LoginDTO;
import com.hbnu.system.model.dto.UserPasswordDTO;
import com.hbnu.system.model.dto.UserSearchPageDTO;
import com.hbnu.system.model.entity.*;
import com.hbnu.system.model.vo.InfoVO;
import com.hbnu.system.model.vo.UserVO;
import com.hbnu.system.service.*;
import com.hbnu.system.utils.JwtUtils;
import com.hbnu.system.utils.MD5Util;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ${author}
 * @since 2023-05-16
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {
    @Autowired
    private IAuthorityService iAuthorityService;

    @Autowired
    private IUserService iUserService;


    @Autowired
    private IDeptService iDeptService;

    @Autowired
    private IPaperService iPaperService;

    @Autowired
    private IPatentService iPatentService;

    @Autowired
    private IWorkService iWorkService;

    @Autowired
    private IArtService iArtService;

    @Autowired
    private IAwardService iAwardService;

    @Autowired
    private IPorproject_registrationService iPorproject_registrationService;

    @Autowired
    private ITranproject_registrationService iTranproject_registrationService;
    @Autowired
    private IdToStatsService idToStatsService;

    @Autowired
    private AchievementService achievementService;

    @Autowired
    private TPlatformService tPlatformService;

    @Autowired
    private RestTemplate restTemplate;


    @Override
    public String loginUser(LoginDTO loginDTO) {
        String username = loginDTO.getUsername();
        String password = loginDTO.getPassword();



        if (StrUtil.isBlank(username) || StrUtil.isBlank(password)) {
            throw new BaseException("用户名或密码为空");
        }

        User one = super.getOne(Wrappers.lambdaQuery(User.class).eq(User::getAccount, loginDTO.getUsername()));

        if (Objects.isNull(one)) {
            throw new BaseException(("用户名或密码错误"));
        }

        if (!one.getPassword().equals(password)) {
            throw new BaseException("用户名或密码错误");
        }

        one.setToken(JwtUtils.getJwtToken(one.getUid(), one.getRole()));
        one.setLogin_status(1);
        return JwtUtils.getJwtToken(one.getUid(), one.getRole());
    }

    @Override
    public InfoVO getInfo(String token) {
        InfoVO info = JwtUtils.getInfo(token);
        String[] strings = new String[1];
        strings[0] = iAuthorityService.getById(info.getRoles()[0]).getText();
        User user = super.getById(info.getUid());

        info.setRoles(strings);
        info.setName(user.getName());
        info.setDept(user.getDept());
        info.setDeptName(idToStatsService.IdToDept(user.getDept()));
        info.setAccount(user.getAccount());
        return info;
    }

    @Override
    public boolean insertUser(User user) {
        if (Objects.nonNull(super.getOne(Wrappers.lambdaQuery(User.class).eq(User::getAccount,user.getAccount())))){
            throw new BaseException("该用户已添加!");
        }
        //如果数据库不存在该数据就进行添加

/*        //进行权限判断
        if(StrUtil.isNotBlank(user.getRole())){
            Authority byId = iAuthorityService.getById(user.getRole());
            if(Objects.isNull(byId)){
                throw new BaseException("权限不存在!");
            }
        }*/

        //进行院系判断
        if(StrUtil.isNotBlank(user.getDept())){
            Dept byId = iDeptService.getById(user.getDept());
            if(Objects.isNull(byId)){
                throw new BaseException("院系不存在!");
            }
        }

        user.setPassword(MD5Util.encrypt("kyc123456"));
        //将用户权限转化为权限编码
        if(user.getRole().equals(StatusCheckConstant.SCHOOL_CHECK_PEOPLE)){
            user.setRole(RoleConstant.ROLE_ADMIN_SCHOOL);
        } else if (user.getRole().equals(StatusCheckConstant.COLLEGE_CHECK_PEOPLE)) {
            user.setRole(RoleConstant.ROLE_ADMIN_COLLEGE);
        }else {
            user.setRole(RoleConstant.ROLE_USER);
        }

        return baseMapper.insert(user) == 1;

    }
    @Override
    public User getUserByAccount(String account){
        User user = iUserService.getOne(Wrappers.lambdaQuery(User.class).eq(User::getAccount,account));
        if(Objects.isNull(user)){
            throw  new BaseException("输入的工号有误！");
        }
        return user;
    }



    @Override
    public List<User> getUserListByAccount(String account) {

        // 参数校验
        if (StringUtils.isBlank(account)) {
            throw new BaseException("工号不能为空");
        }

        // 查询用户列表
        List<User> users = baseMapper.selectList(Wrappers.lambdaQuery(User.class)
                .like(User::getAccount, account));

        if (CollectionUtils.isEmpty(users)) {
            throw new BaseException("输入的工号有误!");
        }

        // 提取所有部门ID
        Set<String> deptIds = users.stream()
                .map(User::getDept)
                .filter(Objects::nonNull)
                .collect(Collectors.toSet());

        if (!deptIds.isEmpty()) {
            // 批量查询部门信息
            Map<String, Dept> deptMap = iDeptService.listByIds(deptIds).stream()
                    .collect(Collectors.toMap(Dept::getDeptID, dept -> dept));

            // 填充部门名称
            users.forEach(user -> {
                String deptId = user.getDept();
                if (deptId != null) {
                    Dept dept = deptMap.get(deptId);
                    if (dept != null) {
                        user.setDept(dept.getDeptName());
                    }
                }
            });
        }

        return users;

    }

    @Override
    public List<List<Integer>> getTotalAchievement(String token) {
        InfoVO info = JwtUtils.getInfo(token);
        String role = iAuthorityService.getById(info.getRoles()[0]).getText();
        User user = iUserService.getById(info.getUid());

        String uid = user.getUid();
        String deptName = null;

        // 根据角色确定部门名称
        if ("院级审核员".equals(role)) {
            deptName = user.getDept();
        }

        List<List<Integer>> resultList = new ArrayList<>();
        List<Integer> passList = new ArrayList<>();
        List<Integer> schoolList = new ArrayList<>();
        List<Integer> collegeList = new ArrayList<>();
        List<Integer> draftList = new ArrayList<>(); // 草稿状态统计

        // 根据不同角色进行统计
        if ("院级教师".equals(role)) {
            // 用户级别的统计
            countUserAchievements(uid, passList, schoolList, collegeList, draftList);
        } else if ("院级审核员".equals(role)) {
            // 部门级别的统计
            countDeptAchievements(deptName, passList, schoolList, collegeList, draftList);
        } else if ("校级审核员".equals(role)) {
            // 全局级别的统计
            countAllAchievements(passList, schoolList, collegeList, draftList);
        }

        resultList.add(passList);
        resultList.add(schoolList);
        resultList.add(collegeList);
        resultList.add(draftList);

        return resultList;
    }

    /**
     * 统计用户级别的科研成果
     */
    private void countUserAchievements(String uid, List<Integer> passList, List<Integer> schoolList, List<Integer> collegeList, List<Integer> draftList) {
        // 论文统计
        countUserPapers(uid, passList, schoolList, collegeList, draftList);

        // 著作统计
        countUserWorks(uid, passList, schoolList, collegeList, draftList);

        // 专利统计
        countUserPatents(uid, passList, schoolList, collegeList, draftList);

        // 成果统计
        countUserAwards(uid, passList, schoolList, collegeList, draftList);

        // 艺术作品统计
        countUserArts(uid, passList, schoolList, collegeList, draftList);

        //咨询报告统计
        countUserConsultations(uid, passList, schoolList, collegeList, draftList);

        //科研平台统计
        countUserPlatforms(uid, passList, schoolList, collegeList, draftList);
    }

    /**
     * 统计部门级别的科研成果
     */
    private void countDeptAchievements(String deptName, List<Integer> passList, List<Integer> schoolList, List<Integer> collegeList, List<Integer> draftList) {
        // 论文统计
        countDeptPapers(deptName, passList, schoolList, collegeList, draftList);

        // 著作统计
        countDeptWorks(deptName, passList, schoolList, collegeList, draftList);

        // 专利统计
        countDeptPatents(deptName, passList, schoolList, collegeList, draftList);

        // 成果统计
        countDeptAwards(deptName, passList, schoolList, collegeList, draftList);

        // 艺术作品统计
        countDeptArts(deptName, passList, schoolList, collegeList, draftList);

        // 咨询报告统计
        countDeptConsultations(deptName, passList, schoolList, collegeList, draftList);

        // 科研平台统计
        countDeptPlatforms(deptName, passList, schoolList, collegeList, draftList);
    }

    /**
     * 统计全局级别的科研成果
     */
    private void countAllAchievements(List<Integer> passList, List<Integer> schoolList, List<Integer> collegeList, List<Integer> draftList) {
        // 论文统计
        countAllPapers(passList, schoolList, collegeList, draftList);

        // 著作统计
        countAllWorks(passList, schoolList, collegeList, draftList);

        // 专利统计
        countAllPatents(passList, schoolList, collegeList,draftList);

        // 成果统计
        countAllAwards(passList, schoolList, collegeList,draftList);

        // 艺术作品统计
        countAllArts(passList, schoolList, collegeList,draftList);

        // 咨询报告统计
        countAllConsultations(passList, schoolList, collegeList,draftList);

        // 科研平台统计
        countAllPlatforms(passList, schoolList, collegeList,draftList);
    }

    // 用户级别论文统计
    private void countUserPapers(String uid, List<Integer> passList, List<Integer> schoolList, List<Integer> collegeList, List<Integer> draftList) {
        passList.add(iPaperService.count(Wrappers.lambdaQuery(Paper.class)
                .eq(Paper::getStatus, StatusCheckConstant.CHECK_PASS)
                .and(wrapper -> wrapper.eq(Paper::getEntered, uid)
                        .or().like(Paper::getAllAuthor, uid))));

        collegeList.add(iPaperService.count(Wrappers.lambdaQuery(Paper.class)
                .eq(Paper::getStatus, StatusCheckConstant.WAIT_COLLEGE_CHECK)
                .and(wrapper -> wrapper.eq(Paper::getEntered, uid)
                        .or().like(Paper::getAllAuthor, uid))));

        schoolList.add(iPaperService.count(Wrappers.lambdaQuery(Paper.class)
                .eq(Paper::getStatus, StatusCheckConstant.WAIT_SCHOOL_CHECK)
                .and(wrapper -> wrapper.eq(Paper::getEntered, uid)
                        .or().like(Paper::getAllAuthor, uid))));

        draftList.add(iPaperService.count(Wrappers.lambdaQuery(Paper.class)
                .eq(Paper::getStatus, StatusCheckConstant.DRAFT)
                .and(wrapper -> wrapper.eq(Paper::getEntered, uid)
                        .or().like(Paper::getAllAuthor, uid))));
    }

    // 部门级别论文统计
    private void countDeptPapers(String deptName, List<Integer> passList, List<Integer> schoolList, List<Integer> collegeList, List<Integer> draftList) {
        passList.add(iPaperService.count(Wrappers.lambdaQuery(Paper.class)
                .eq(Paper::getStatus, StatusCheckConstant.CHECK_PASS)
                .eq(Paper::getDept, deptName)));

        collegeList.add(iPaperService.count(Wrappers.lambdaQuery(Paper.class)
                .eq(Paper::getStatus, StatusCheckConstant.WAIT_COLLEGE_CHECK)
                .eq(Paper::getDept, deptName)));

        schoolList.add(iPaperService.count(Wrappers.lambdaQuery(Paper.class)
                .eq(Paper::getStatus, StatusCheckConstant.WAIT_SCHOOL_CHECK)
                .eq(Paper::getDept, deptName)));

        draftList.add(iPaperService.count(Wrappers.lambdaQuery(Paper.class)
                .eq(Paper::getStatus, StatusCheckConstant.DRAFT)
                .eq(Paper::getDept, deptName)));
    }

    // 全局级别论文统计
    private void countAllPapers(List<Integer> passList, List<Integer> schoolList, List<Integer> collegeList, List<Integer> draftList) {
        passList.add(iPaperService.count(Wrappers.lambdaQuery(Paper.class)
                .eq(Paper::getStatus, StatusCheckConstant.CHECK_PASS)));

        collegeList.add(iPaperService.count(Wrappers.lambdaQuery(Paper.class)
                .eq(Paper::getStatus, StatusCheckConstant.WAIT_COLLEGE_CHECK)));

        schoolList.add(iPaperService.count(Wrappers.lambdaQuery(Paper.class)
                .eq(Paper::getStatus, StatusCheckConstant.WAIT_SCHOOL_CHECK)));

        draftList.add(iPaperService.count(Wrappers.lambdaQuery(Paper.class)
                .eq(Paper::getStatus, StatusCheckConstant.DRAFT)));
    }

    // 用户级别著作统计
    private void countUserWorks(String uid, List<Integer> passList, List<Integer> schoolList, List<Integer> collegeList, List<Integer> draftList) {
        passList.add(iWorkService.count(Wrappers.lambdaQuery(Work.class)
                .eq(Work::getStatus, StatusCheckConstant.CHECK_PASS)
                .and(wrapper -> wrapper.eq(Work::getEntered, uid)
                        .or().like(Work::getAllauthor, uid))));

        collegeList.add(iWorkService.count(Wrappers.lambdaQuery(Work.class)
                .eq(Work::getStatus, StatusCheckConstant.WAIT_COLLEGE_CHECK)
                .and(wrapper -> wrapper.eq(Work::getEntered, uid)
                        .or().like(Work::getAllauthor, uid))));

        schoolList.add(iWorkService.count(Wrappers.lambdaQuery(Work.class)
                .eq(Work::getStatus, StatusCheckConstant.WAIT_SCHOOL_CHECK)
                .and(wrapper -> wrapper.eq(Work::getEntered, uid)
                        .or().like(Work::getAllauthor, uid))));

        draftList.add(iWorkService.count(Wrappers.lambdaQuery(Work.class)
                .eq(Work::getStatus, StatusCheckConstant.DRAFT)
                .and(wrapper -> wrapper.eq(Work::getEntered, uid)
                        .or().like(Work::getAllauthor, uid))));
    }

    // 部门级别著作统计
    private void countDeptWorks(String deptName, List<Integer> passList, List<Integer> schoolList, List<Integer> collegeList, List<Integer> draftList) {
        passList.add(iWorkService.count(Wrappers.lambdaQuery(Work.class)
                .eq(Work::getStatus, StatusCheckConstant.CHECK_PASS)
                .eq(Work::getDept, deptName)));

        collegeList.add(iWorkService.count(Wrappers.lambdaQuery(Work.class)
                .eq(Work::getStatus, StatusCheckConstant.WAIT_COLLEGE_CHECK)
                .eq(Work::getDept, deptName)));

        schoolList.add(iWorkService.count(Wrappers.lambdaQuery(Work.class)
                .eq(Work::getStatus, StatusCheckConstant.WAIT_SCHOOL_CHECK)
                .eq(Work::getDept, deptName)));

        draftList.add(iWorkService.count(Wrappers.lambdaQuery(Work.class)
                .eq(Work::getStatus, StatusCheckConstant.DRAFT)
                .eq(Work::getDept, deptName)));
    }

    // 全局级别著作统计
    private void countAllWorks(List<Integer> passList, List<Integer> schoolList, List<Integer> collegeList, List<Integer> draftList) {
        passList.add(iWorkService.count(Wrappers.lambdaQuery(Work.class)
                .eq(Work::getStatus, StatusCheckConstant.CHECK_PASS)));

        collegeList.add(iWorkService.count(Wrappers.lambdaQuery(Work.class)
                .eq(Work::getStatus, StatusCheckConstant.WAIT_COLLEGE_CHECK)));

        schoolList.add(iWorkService.count(Wrappers.lambdaQuery(Work.class)
                .eq(Work::getStatus, StatusCheckConstant.WAIT_SCHOOL_CHECK)));

        draftList.add(iWorkService.count(Wrappers.lambdaQuery(Work.class)
                .eq(Work::getStatus, StatusCheckConstant.DRAFT)));
    }

    // 用户级别专利统计
    private void countUserPatents(String uid, List<Integer> passList, List<Integer> schoolList, List<Integer> collegeList, List<Integer> draftList) {
        passList.add(iPatentService.count(Wrappers.lambdaQuery(Patent.class)
                .eq(Patent::getStatus, StatusCheckConstant.CHECK_PASS)
                .and(wrapper -> wrapper.eq(Patent::getEntered, uid)
                        .or().like(Patent::getAllAuthor, uid))));

        collegeList.add(iPatentService.count(Wrappers.lambdaQuery(Patent.class)
                .eq(Patent::getStatus, StatusCheckConstant.WAIT_COLLEGE_CHECK)
                .and(wrapper -> wrapper.eq(Patent::getEntered, uid)
                        .or().like(Patent::getAllAuthor, uid))));

        schoolList.add(iPatentService.count(Wrappers.lambdaQuery(Patent.class)
                .eq(Patent::getStatus, StatusCheckConstant.WAIT_SCHOOL_CHECK)
                .and(wrapper -> wrapper.eq(Patent::getEntered, uid)
                        .or().like(Patent::getAllAuthor, uid))));

        draftList.add(iPatentService.count(Wrappers.lambdaQuery(Patent.class)
                .eq(Patent::getStatus, StatusCheckConstant.DRAFT)
                .and(wrapper -> wrapper.eq(Patent::getEntered, uid)
                        .or().like(Patent::getAllAuthor, uid))));
    }

    // 部门级别专利统计
    private void countDeptPatents(String deptName, List<Integer> passList, List<Integer> schoolList, List<Integer> collegeList, List<Integer> draftList) {
        passList.add(iPatentService.count(Wrappers.lambdaQuery(Patent.class)
                .eq(Patent::getStatus, StatusCheckConstant.CHECK_PASS)
                .eq(Patent::getDept, deptName)));

        collegeList.add(iPatentService.count(Wrappers.lambdaQuery(Patent.class)
                .eq(Patent::getStatus, StatusCheckConstant.WAIT_COLLEGE_CHECK)
                .eq(Patent::getDept, deptName)));

        schoolList.add(iPatentService.count(Wrappers.lambdaQuery(Patent.class)
                .eq(Patent::getStatus, StatusCheckConstant.WAIT_SCHOOL_CHECK)
                .eq(Patent::getDept, deptName)));

        draftList.add(iPatentService.count(Wrappers.lambdaQuery(Patent.class)
                .eq(Patent::getStatus, StatusCheckConstant.DRAFT)
                .eq(Patent::getDept, deptName)));
    }

    // 全局级别专利统计
    private void countAllPatents(List<Integer> passList, List<Integer> schoolList, List<Integer> collegeList, List<Integer> draftList) {
        passList.add(iPatentService.count(Wrappers.lambdaQuery(Patent.class)
                .eq(Patent::getStatus, StatusCheckConstant.CHECK_PASS)));

        collegeList.add(iPatentService.count(Wrappers.lambdaQuery(Patent.class)
                .eq(Patent::getStatus, StatusCheckConstant.WAIT_COLLEGE_CHECK)));

        schoolList.add(iPatentService.count(Wrappers.lambdaQuery(Patent.class)
                .eq(Patent::getStatus, StatusCheckConstant.WAIT_SCHOOL_CHECK)));

        draftList.add(iPatentService.count(Wrappers.lambdaQuery(Patent.class)
                .eq(Patent::getStatus, StatusCheckConstant.DRAFT)));
    }

    // 用户级别成果统计
    private void countUserAwards(String uid, List<Integer> passList, List<Integer> schoolList, List<Integer> collegeList, List<Integer> draftList) {
        passList.add(iAwardService.count(Wrappers.lambdaQuery(Award.class)
                .eq(Award::getStatus, StatusCheckConstant.CHECK_PASS)
                .and(wrapper -> wrapper.eq(Award::getEntered, uid)
                        .or().like(Award::getAllAuthor, uid))));

        collegeList.add(iAwardService.count(Wrappers.lambdaQuery(Award.class)
                .eq(Award::getStatus, StatusCheckConstant.WAIT_COLLEGE_CHECK)
                .and(wrapper -> wrapper.eq(Award::getEntered, uid)
                        .or().like(Award::getAllAuthor, uid))));

        schoolList.add(iAwardService.count(Wrappers.lambdaQuery(Award.class)
                .eq(Award::getStatus, StatusCheckConstant.WAIT_SCHOOL_CHECK)
                .and(wrapper -> wrapper.eq(Award::getEntered, uid)
                        .or().like(Award::getAllAuthor, uid))));

        draftList.add(iAwardService.count(Wrappers.lambdaQuery(Award.class)
                .eq(Award::getStatus, StatusCheckConstant.DRAFT)
                .and(wrapper -> wrapper.eq(Award::getEntered, uid)
                        .or().like(Award::getAllAuthor, uid))));
    }

    // 部门级别成果统计
    private void countDeptAwards(String deptName, List<Integer> passList, List<Integer> schoolList, List<Integer> collegeList, List<Integer> draftList) {
        passList.add(iAwardService.count(Wrappers.lambdaQuery(Award.class)
                .eq(Award::getStatus, StatusCheckConstant.CHECK_PASS)
                .eq(Award::getDept, deptName)));

        collegeList.add(iAwardService.count(Wrappers.lambdaQuery(Award.class)
                .eq(Award::getStatus, StatusCheckConstant.WAIT_COLLEGE_CHECK)
                .eq(Award::getDept, deptName)));

        schoolList.add(iAwardService.count(Wrappers.lambdaQuery(Award.class)
                .eq(Award::getStatus, StatusCheckConstant.WAIT_SCHOOL_CHECK)
                .eq(Award::getDept, deptName)));

        draftList.add(iAwardService.count(Wrappers.lambdaQuery(Award.class)
                .eq(Award::getStatus, StatusCheckConstant.DRAFT)
                .eq(Award::getDept, deptName)));
    }

    // 全局级别成果统计
    private void countAllAwards(List<Integer> passList, List<Integer> schoolList, List<Integer> collegeList, List<Integer> draftList) {
        passList.add(iAwardService.count(Wrappers.lambdaQuery(Award.class)
                .eq(Award::getStatus, StatusCheckConstant.CHECK_PASS)));

        collegeList.add(iAwardService.count(Wrappers.lambdaQuery(Award.class)
                .eq(Award::getStatus, StatusCheckConstant.WAIT_COLLEGE_CHECK)));

        schoolList.add(iAwardService.count(Wrappers.lambdaQuery(Award.class)
                .eq(Award::getStatus, StatusCheckConstant.WAIT_SCHOOL_CHECK)));

        draftList.add(iAwardService.count(Wrappers.lambdaQuery(Award.class)
                .eq(Award::getStatus, StatusCheckConstant.DRAFT)));
    }

    // 用户级别艺术作品统计
    private void countUserArts(String uid, List<Integer> passList, List<Integer> schoolList, List<Integer> collegeList, List<Integer> draftList) {
        passList.add(iArtService.count(Wrappers.lambdaQuery(Art.class)
                .eq(Art::getStatus, StatusCheckConstant.CHECK_PASS)
                .and(wrapper -> wrapper.eq(Art::getEntered, uid)
                        .or().like(Art::getAllAuthor, uid))));

        collegeList.add(iArtService.count(Wrappers.lambdaQuery(Art.class)
                .eq(Art::getStatus, StatusCheckConstant.WAIT_COLLEGE_CHECK)
                .and(wrapper -> wrapper.eq(Art::getEntered, uid)
                        .or().like(Art::getAllAuthor, uid))));

        schoolList.add(iArtService.count(Wrappers.lambdaQuery(Art.class)
                .eq(Art::getStatus, StatusCheckConstant.WAIT_SCHOOL_CHECK)
                .and(wrapper -> wrapper.eq(Art::getEntered, uid)
                        .or().like(Art::getAllAuthor, uid))));

        draftList.add(iArtService.count(Wrappers.lambdaQuery(Art.class)
                .eq(Art::getStatus, StatusCheckConstant.DRAFT)
                .and(wrapper -> wrapper.eq(Art::getEntered, uid)
                        .or().like(Art::getAllAuthor, uid))));
    }

    // 部门级别咨询报告统计
    private void countUserConsultations(String uid, List<Integer> passList, List<Integer> schoolList, List<Integer> collegeList, List<Integer> draftList) {
        passList.add(achievementService.count(Wrappers.lambdaQuery(Achievement.class)
                .eq(Achievement::getStatus, StatusCheckConstant.CHECK_PASS)
                .and(wrapper -> wrapper.eq(Achievement::getEntered, uid)
                        .or().like(Achievement::getAllAuthor, uid))));

        collegeList.add(achievementService.count(Wrappers.lambdaQuery(Achievement.class)
                .eq(Achievement::getStatus, StatusCheckConstant.WAIT_COLLEGE_CHECK)
                .and(wrapper -> wrapper.eq(Achievement::getEntered, uid)
                        .or().like(Achievement::getAllAuthor, uid))));

        schoolList.add(achievementService.count(Wrappers.lambdaQuery(Achievement.class)
                .eq(Achievement::getStatus, StatusCheckConstant.WAIT_SCHOOL_CHECK)
                .and(wrapper -> wrapper.eq(Achievement::getEntered, uid)
                        .or().like(Achievement::getAllAuthor, uid))));

        draftList.add(achievementService.count(Wrappers.lambdaQuery(Achievement.class)
                .eq(Achievement::getStatus, StatusCheckConstant.DRAFT)
                .and(wrapper -> wrapper.eq(Achievement::getEntered, uid)
                        .or().like(Achievement::getAllAuthor, uid))));
    }

    // 部门级别科研平台统计
    private void countUserPlatforms(String uid, List<Integer> passList, List<Integer> schoolList, List<Integer> collegeList, List<Integer> draftList) {
        passList.add(tPlatformService.count(Wrappers.lambdaQuery(TPlatform.class)
                .eq(TPlatform::getStatus, StatusCheckConstant.CHECK_PASS)
                .and(wrapper -> wrapper.eq(TPlatform::getEntered, uid)
                        .or().like(TPlatform::getEntered, uid))));

        collegeList.add(tPlatformService.count(Wrappers.lambdaQuery(TPlatform.class)
                .eq(TPlatform::getStatus, StatusCheckConstant.WAIT_COLLEGE_CHECK)
                .and(wrapper -> wrapper.eq(TPlatform::getEntered, uid)
                        .or().like(TPlatform::getEntered, uid))));

        schoolList.add(tPlatformService.count(Wrappers.lambdaQuery(TPlatform.class)
                .eq(TPlatform::getStatus, StatusCheckConstant.WAIT_SCHOOL_CHECK)
                .and(wrapper -> wrapper.eq(TPlatform::getEntered, uid)
                        .or().like(TPlatform::getEntered, uid))));

        draftList.add(tPlatformService.count(Wrappers.lambdaQuery(TPlatform.class)
                .eq(TPlatform::getStatus, StatusCheckConstant.DRAFT)
                .and(wrapper -> wrapper.eq(TPlatform::getEntered, uid)
                        .or().like(TPlatform::getEntered, uid))));
    }

    // 部门级别艺术作品统计
    private void countDeptArts(String deptName, List<Integer> passList, List<Integer> schoolList, List<Integer> collegeList, List<Integer> draftList) {
        passList.add(iArtService.count(Wrappers.lambdaQuery(Art.class)
                .eq(Art::getStatus, StatusCheckConstant.CHECK_PASS)
                .eq(Art::getDept, deptName)));

        collegeList.add(iArtService.count(Wrappers.lambdaQuery(Art.class)
                .eq(Art::getStatus, StatusCheckConstant.WAIT_COLLEGE_CHECK)
                .eq(Art::getDept, deptName)));

        schoolList.add(iArtService.count(Wrappers.lambdaQuery(Art.class)
                .eq(Art::getStatus, StatusCheckConstant.WAIT_SCHOOL_CHECK)
                .eq(Art::getDept, deptName)));

        draftList.add(iArtService.count(Wrappers.lambdaQuery(Art.class)
                .eq(Art::getStatus, StatusCheckConstant.DRAFT)
                .eq(Art::getDept, deptName)));
    }

    // 部门级别咨询报告统计
    private void countDeptConsultations(String deptName, List<Integer> passList, List<Integer> schoolList, List<Integer> collegeList, List<Integer> draftList) {
        passList.add(achievementService.count(Wrappers.lambdaQuery(Achievement.class)
                .eq(Achievement::getStatus, StatusCheckConstant.CHECK_PASS)
                .eq(Achievement::getInstitutionName, deptName)));

        collegeList.add(achievementService.count(Wrappers.lambdaQuery(Achievement.class)
                .eq(Achievement::getStatus, StatusCheckConstant.WAIT_COLLEGE_CHECK)
                .eq(Achievement::getInstitutionName, deptName)));

        schoolList.add(achievementService.count(Wrappers.lambdaQuery(Achievement.class)
                .eq(Achievement::getStatus, StatusCheckConstant.WAIT_SCHOOL_CHECK)
                .eq(Achievement::getInstitutionName, deptName)));

        draftList.add(achievementService.count(Wrappers.lambdaQuery(Achievement.class)
                .eq(Achievement::getStatus, StatusCheckConstant.DRAFT)
                .eq(Achievement::getInstitutionName, deptName)));
    }

    private void countDeptPlatforms(String deptName, List<Integer> passList, List<Integer> schoolList, List<Integer> collegeList, List<Integer> draftList) {
        passList.add(tPlatformService.count(Wrappers.lambdaQuery(TPlatform.class)
                .eq(TPlatform::getStatus, StatusCheckConstant.CHECK_PASS)
                .eq(TPlatform::getSupportCollege, deptName)));

        collegeList.add(tPlatformService.count(Wrappers.lambdaQuery(TPlatform.class)
                .eq(TPlatform::getStatus, StatusCheckConstant.WAIT_COLLEGE_CHECK)
                .eq(TPlatform::getSupportCollege, deptName)));

        schoolList.add(tPlatformService.count(Wrappers.lambdaQuery(TPlatform.class)
                .eq(TPlatform::getStatus, StatusCheckConstant.WAIT_SCHOOL_CHECK)
                .eq(TPlatform::getSupportCollege, deptName)));

        draftList.add(tPlatformService.count(Wrappers.lambdaQuery(TPlatform.class)
                .eq(TPlatform::getStatus, StatusCheckConstant.DRAFT)
                .eq(TPlatform::getSupportCollege, deptName)));
    }

    // 全局级别艺术作品统计
    private void countAllArts(List<Integer> passList, List<Integer> schoolList, List<Integer> collegeList, List<Integer> draftList) {
        passList.add(iArtService.count(Wrappers.lambdaQuery(Art.class)
                .eq(Art::getStatus, StatusCheckConstant.CHECK_PASS)));

        collegeList.add(iArtService.count(Wrappers.lambdaQuery(Art.class)
                .eq(Art::getStatus, StatusCheckConstant.WAIT_COLLEGE_CHECK)));

        schoolList.add(iArtService.count(Wrappers.lambdaQuery(Art.class)
                .eq(Art::getStatus, StatusCheckConstant.WAIT_SCHOOL_CHECK)));

        draftList.add(iArtService.count(Wrappers.lambdaQuery(Art.class)
                .eq(Art::getStatus, StatusCheckConstant.DRAFT)));
    }

    // 全局级别咨询报告统计
    private void countAllConsultations(List<Integer> passList, List<Integer> schoolList, List<Integer> collegeList, List<Integer> draftList) {
        passList.add(achievementService.count(Wrappers.lambdaQuery(Achievement.class)
                .eq(Achievement::getStatus, StatusCheckConstant.CHECK_PASS)));

        collegeList.add(achievementService.count(Wrappers.lambdaQuery(Achievement.class)
                .eq(Achievement::getStatus, StatusCheckConstant.WAIT_COLLEGE_CHECK)));

        schoolList.add(achievementService.count(Wrappers.lambdaQuery(Achievement.class)
                .eq(Achievement::getStatus, StatusCheckConstant.WAIT_SCHOOL_CHECK)));

        draftList.add(achievementService.count(Wrappers.lambdaQuery(Achievement.class)
                .eq(Achievement::getStatus, StatusCheckConstant.DRAFT)));
    }

    // 全局级别平台统计
    private void countAllPlatforms(List<Integer> passList, List<Integer> schoolList, List<Integer> collegeList, List<Integer> draftList) {
        passList.add(tPlatformService.count(Wrappers.lambdaQuery(TPlatform.class)
                .eq(TPlatform::getStatus, StatusCheckConstant.CHECK_PASS)));

        collegeList.add(tPlatformService.count(Wrappers.lambdaQuery(TPlatform.class)
                .eq(TPlatform::getStatus, StatusCheckConstant.WAIT_COLLEGE_CHECK)));

        schoolList.add(tPlatformService.count(Wrappers.lambdaQuery(TPlatform.class)
                .eq(TPlatform::getStatus, StatusCheckConstant.WAIT_SCHOOL_CHECK)));

        draftList.add(tPlatformService.count(Wrappers.lambdaQuery(TPlatform.class)
                .eq(TPlatform::getStatus, StatusCheckConstant.DRAFT)));
    }

    @Override
    public List<List<Integer>> getTotalProject(String token) {
        InfoVO info = JwtUtils.getInfo(token);
        String role = iAuthorityService.getById(info.getRoles()[0]).getText();
        User user = iUserService.getById(info.getUid());

        String uid = user.getUid();

        List<Integer> zaishidaiResult = new ArrayList<>();
        List<Integer> zaishiResult = new ArrayList<>();
        List<Integer> jietidaiResult = new ArrayList<>();
        List<Integer> jietiResult = new ArrayList<>();
        List<Integer> draftResult = new ArrayList<>(); // 草稿状态统计
        List<List<Integer>> result = new ArrayList<>();

        if ("院级教师".equals(role)) {
            // 用户级别的项目统计
            countUserProjects(uid, zaishidaiResult, zaishiResult, jietidaiResult, jietiResult, draftResult);
        } else if ("校级审核员".equals(role) || "院级管理员".equals(role)) {
            // 全局级别的项目统计
            countAllProjects(zaishidaiResult, zaishiResult, jietidaiResult, jietiResult, draftResult);
        }

        result.add(zaishidaiResult);
        result.add(zaishiResult);
        result.add(jietidaiResult);
        result.add(jietiResult);
        result.add(draftResult); // 添加草稿统计结果
        return result;
    }


    /**
     * 统计用户级别的项目
     */
    private void countUserProjects(String uid, List<Integer> zaishidaiResult, List<Integer> zaishiResult,
                                   List<Integer> jietidaiResult, List<Integer> jietiResult, List<Integer> draftResult) {
        // 纵向项目统计
        countUserPorProjects(uid, zaishidaiResult, zaishiResult, jietidaiResult, jietiResult, draftResult);

        // 横向项目统计
        countUserTranProjects(uid, zaishidaiResult, zaishiResult, jietidaiResult, jietiResult, draftResult);
    }

    /**
     * 统计全局级别的项目
     */
    private void countAllProjects(List<Integer> zaishidaiResult, List<Integer> zaishiResult,
                                  List<Integer> jietidaiResult, List<Integer> jietiResult, List<Integer> draftResult) {
        // 纵向项目统计
        countAllPorProjects(zaishidaiResult, zaishiResult, jietidaiResult, jietiResult, draftResult);

        // 横向项目统计
        countAllTranProjects(zaishidaiResult, zaishiResult, jietidaiResult, jietiResult, draftResult);
    }

    // 用户级别纵向项目统计
    private void countUserPorProjects(String uid, List<Integer> zaishidaiResult, List<Integer> zaishiResult,
                                      List<Integer> jietidaiResult, List<Integer> jietiResult, List<Integer> draftResult) {
        zaishidaiResult.add(iPorproject_registrationService.count(Wrappers.lambdaQuery(Porproject_registration.class)
                .eq(Porproject_registration::getProjectState, StatusCheckConstant.IN_PROGRESS_WAIT_SCHOOL_CHECK)
                .and(wrapper -> wrapper.eq(Porproject_registration::getEntered, uid)
                        .or().eq(Porproject_registration::getHead, uid)
                        .or().like(Porproject_registration::getProjectMembers, uid))));

        zaishiResult.add(iPorproject_registrationService.count(Wrappers.lambdaQuery(Porproject_registration.class)
                .eq(Porproject_registration::getProjectState, StatusCheckConstant.IN_PROGRESS)
                .and(wrapper -> wrapper.eq(Porproject_registration::getEntered, uid)
                        .or().eq(Porproject_registration::getHead, uid)
                        .or().like(Porproject_registration::getProjectMembers, uid))));

        jietidaiResult.add(iPorproject_registrationService.count(Wrappers.lambdaQuery(Porproject_registration.class)
                .eq(Porproject_registration::getProjectState, StatusCheckConstant.FINISH_WAIT_SCHOOL_CHECK)
                .and(wrapper -> wrapper.eq(Porproject_registration::getEntered, uid)
                        .or().eq(Porproject_registration::getHead, uid)
                        .or().like(Porproject_registration::getProjectMembers, uid))));

        jietiResult.add(iPorproject_registrationService.count(Wrappers.lambdaQuery(Porproject_registration.class)
                .eq(Porproject_registration::getProjectState, StatusCheckConstant.PROJECT_FINISH)
                .and(wrapper -> wrapper.eq(Porproject_registration::getEntered, uid)
                        .or().eq(Porproject_registration::getHead, uid)
                        .or().like(Porproject_registration::getProjectMembers, uid))));

        draftResult.add(iPorproject_registrationService.count(Wrappers.lambdaQuery(Porproject_registration.class)
                .eq(Porproject_registration::getProjectState, StatusCheckConstant.DRAFT)
                .and(wrapper -> wrapper.eq(Porproject_registration::getEntered, uid)
                        .or().eq(Porproject_registration::getHead, uid)
                        .or().like(Porproject_registration::getProjectMembers, uid))));
    }

    // 用户级别横向项目统计
    private void countUserTranProjects(String uid, List<Integer> zaishidaiResult, List<Integer> zaishiResult,
                                       List<Integer> jietidaiResult, List<Integer> jietiResult, List<Integer> draftResult) {
        zaishidaiResult.add(iTranproject_registrationService.count(Wrappers.lambdaQuery(Tranproject_registration.class)
                .eq(Tranproject_registration::getProjectState, StatusCheckConstant.IN_PROGRESS_WAIT_SCHOOL_CHECK)
                .and(wrapper -> wrapper.eq(Tranproject_registration::getEntered, uid)
                        .or().eq(Tranproject_registration::getHead, uid)
                        .or().like(Tranproject_registration::getProjectMembers, uid))));

        zaishiResult.add(iTranproject_registrationService.count(Wrappers.lambdaQuery(Tranproject_registration.class)
                .eq(Tranproject_registration::getProjectState, StatusCheckConstant.IN_PROGRESS)
                .and(wrapper -> wrapper.eq(Tranproject_registration::getEntered, uid)
                        .or().eq(Tranproject_registration::getHead, uid)
                        .or().like(Tranproject_registration::getProjectMembers, uid))));

        jietidaiResult.add(iTranproject_registrationService.count(Wrappers.lambdaQuery(Tranproject_registration.class)
                .eq(Tranproject_registration::getProjectState, StatusCheckConstant.FINISH_WAIT_SCHOOL_CHECK)
                .and(wrapper -> wrapper.eq(Tranproject_registration::getEntered, uid)
                        .or().eq(Tranproject_registration::getHead, uid)
                        .or().like(Tranproject_registration::getProjectMembers, uid))));

        jietiResult.add(iTranproject_registrationService.count(Wrappers.lambdaQuery(Tranproject_registration.class)
                .eq(Tranproject_registration::getProjectState, StatusCheckConstant.PROJECT_FINISH)
                .and(wrapper -> wrapper.eq(Tranproject_registration::getEntered, uid)
                        .or().eq(Tranproject_registration::getHead, uid)
                        .or().like(Tranproject_registration::getProjectMembers, uid))));

        draftResult.add(iTranproject_registrationService.count(Wrappers.lambdaQuery(Tranproject_registration.class)
                .eq(Tranproject_registration::getProjectState, StatusCheckConstant.DRAFT)
                .and(wrapper -> wrapper.eq(Tranproject_registration::getEntered, uid)
                        .or().eq(Tranproject_registration::getHead, uid)
                        .or().like(Tranproject_registration::getProjectMembers, uid))));
    }

    // 全局级别纵向项目统计
    private void countAllPorProjects(List<Integer> zaishidaiResult, List<Integer> zaishiResult,
                                     List<Integer> jietidaiResult, List<Integer> jietiResult, List<Integer> draftResult) {
        zaishidaiResult.add(iPorproject_registrationService.count(Wrappers.lambdaQuery(Porproject_registration.class)
                .eq(Porproject_registration::getProjectState, "项目在研待审核")));

        zaishiResult.add(iPorproject_registrationService.count(Wrappers.lambdaQuery(Porproject_registration.class)
                .eq(Porproject_registration::getProjectState, "项目在研")));

        jietidaiResult.add(iPorproject_registrationService.count(Wrappers.lambdaQuery(Porproject_registration.class)
                .eq(Porproject_registration::getProjectState, "项目结题待审核")));

        jietiResult.add(iPorproject_registrationService.count(Wrappers.lambdaQuery(Porproject_registration.class)
                .eq(Porproject_registration::getProjectState, "项目结题")));

        draftResult.add(iPorproject_registrationService.count(Wrappers.lambdaQuery(Porproject_registration.class)
                .eq(Porproject_registration::getProjectState, "草稿")));
    }

    // 全局级别横向项目统计
    private void countAllTranProjects(List<Integer> zaishidaiResult, List<Integer> zaishiResult,
                                      List<Integer> jietidaiResult, List<Integer> jietiResult, List<Integer> draftResult) {
        zaishidaiResult.add(iTranproject_registrationService.count(Wrappers.lambdaQuery(Tranproject_registration.class)
                .eq(Tranproject_registration::getProjectState, "项目在研待审核")));

        zaishiResult.add(iTranproject_registrationService.count(Wrappers.lambdaQuery(Tranproject_registration.class)
                .eq(Tranproject_registration::getProjectState, "项目在研")));

        jietidaiResult.add(iTranproject_registrationService.count(Wrappers.lambdaQuery(Tranproject_registration.class)
                .eq(Tranproject_registration::getProjectState, "结题待审核")));

        jietiResult.add(iTranproject_registrationService.count(Wrappers.lambdaQuery(Tranproject_registration.class)
                .eq(Tranproject_registration::getProjectState, "项目结题")));

        draftResult.add(iTranproject_registrationService.count(Wrappers.lambdaQuery(Tranproject_registration.class)
                .eq(Tranproject_registration::getProjectState, "草稿")));
    }


    /**
     * 获取用户列表
     *
     * @param userSearchPageDTO
     * @return
     */
    @Override
    public CommonPage<UserVO> getUserList(UserSearchPageDTO userSearchPageDTO) {
        Page<User> page = new Page<>(userSearchPageDTO.getPageNum(), userSearchPageDTO.getPageSize());
        // 创建查询条件
        LambdaQueryWrapper<User> wrapper = Wrappers.lambdaQuery(User.class)
                .eq(StringUtils.isNotBlank(userSearchPageDTO.getDept()), User::getDept, userSearchPageDTO.getDept())
                .like(StringUtils.isNotBlank(userSearchPageDTO.getAccount()), User::getAccount, userSearchPageDTO.getAccount())
                .like(StringUtils.isNotBlank(userSearchPageDTO.getName()), User::getName, userSearchPageDTO.getName());

        // 执行查询
        Page<User> userPage = baseMapper.selectPage(page, wrapper);

        // 转换为 VO 分页对象
        List<UserVO> userVOList = userPage.getRecords().stream()
                .map(user -> {
                    UserVO vo = new UserVO();
                    vo.setName(user.getName());
                    vo.setAccount(user.getAccount());
                    vo.setId(user.getUid());
                    vo.setDept(user.getDept());
                    //根据role编码获取权限名称，通过枚举类RoleEnum
                    vo.setRole(RoleEnum.getByCode(user.getRole()).getName());
                    vo.setDeptName(idToStatsService.IdToDept(user.getDept()));
                    return vo;
                })
                .collect(Collectors.toList());

        CommonPage<UserVO> commonPage = new CommonPage<>();
        commonPage.setList(userVOList);
        commonPage.setTotal(userPage.getTotal());
        commonPage.setPageNum(userPage.getCurrent());
        commonPage.setPageSize(userPage.getSize());

        return commonPage;
    }

    /**
     * 修改用户
     *
     * @param user
     */
    @Override
    public void updateUser(User user) {
        //查询工号是否改变
        if(!user.getAccount().equals(baseMapper.selectById(user.getUid()).getAccount())){
            // 工号改变
            //根据工号查询是否有多个工号
            List<User> userList = baseMapper.selectList(Wrappers.lambdaQuery(User.class).eq(User::getAccount, user.getAccount()));
            if(userList.size() > 0){
                throw new BaseException("工号重复!");

            }
        }
        //将用户权限转化为权限编码
        if(user.getRole().equals(StatusCheckConstant.SCHOOL_CHECK_PEOPLE)){
            user.setRole(RoleConstant.ROLE_ADMIN_SCHOOL);
        } else if (user.getRole().equals(StatusCheckConstant.COLLEGE_CHECK_PEOPLE)) {
            user.setRole(RoleConstant.ROLE_ADMIN_COLLEGE);
        }else {
            user.setRole(RoleConstant.ROLE_USER);
        }
        iUserService.updateById(user);
    }

    /**
     * 修改密码
     *
     * @param userPasswordDTO 修改密码参数
     */
    @Override
    public void updatePassword(UserPasswordDTO userPasswordDTO) {
        User user = getById(userPasswordDTO.getUid());

        user.setPassword(userPasswordDTO.getNewPassword());
        baseMapper.updateById(user);
    }

    /**
     * 同步所有用户
     */
    @Override
    public void syncAllUser() {
        // 对用户的信息进行批量更新，用户存在则进行更新操作，没有该用户则进行插入操作
        ResponseEntity<List<User>> response = restTemplate.exchange(
                "http://localhost:9191/sync-user",
                HttpMethod.GET,
                null,
                new ParameterizedTypeReference<List<User>>() {
                }
        );
        // 2.2.解析响应
        if(!response.getStatusCode().is2xxSuccessful()){
            // 查询失败，直接结束
            return;
        }
        // 获取响应体——最新的用户数据
        List<User> userList = response.getBody();

        if (userList == null || userList.isEmpty()) {
            throw new BaseException("未获取到用户同步数据");
        }

        // 获取所有现有的用户账户信息，用于判断是更新还是插入
        List<User> existingUsers = this.list();
        Map<String, User> existingUserMap = existingUsers.stream()
                .collect(Collectors.toMap(User::getAccount, user -> user, (existing, replacement) -> existing));

        // 分别处理需要更新和插入的用户
        List<User> usersToInsert = new ArrayList<>();
        List<User> usersToUpdate = new ArrayList<>();

        for (User syncUser : userList) {
            User user = new User();
            // 映射User到User实体
            user.setAccount(syncUser.getAccount());
            user.setName(syncUser.getName());
            user.setDept(syncUser.getDept());
            user.setPhone(syncUser.getPhone());
            user.setEmail(syncUser.getEmail());

            // 设置默认密码（如果用户不存在）
            if (!existingUserMap.containsKey(user.getAccount())) {
                user.setPassword("kyc123456"); // 默认密码
                user.setRole(RoleConstant.ROLE_USER); // 默认角色为普通用户
                usersToInsert.add(user);
            } else {
                // 更新现有用户信息，但保留原有密码和角色
                User existingUser = existingUserMap.get(user.getAccount());
                user.setUid(existingUser.getUid()); // 保留原有ID
                user.setPassword(existingUser.getPassword()); // 保留原有密码
                user.setRole(existingUser.getRole()); // 保留原有角色
                usersToUpdate.add(user);
            }
        }

        // 批量插入新用户
        if (!usersToInsert.isEmpty()) {
            this.saveBatch(usersToInsert);
        }

        // 批量更新现有用户
        if (!usersToUpdate.isEmpty()) {
            this.updateBatchById(usersToUpdate);
        }
    }

    /**
     * 批量加密所有用户密码
     * @return 加密成功的用户数量
     */
    @Override
    public int batchEncryptPassword() {
        // 查询所有用户
        List<User> allUsers = this.list();
        if (allUsers == null || allUsers.isEmpty()) {
            return 0;
        }
        
        // 记录需要更新的用户
        List<User> usersToUpdate = new ArrayList<>();
        int encryptedCount = 0;
        
        // 对每个用户的密码进行加密处理
        for (User user : allUsers) {
            String password = user.getPassword();
            // 检查密码是否已经加密（MD5Util.encrypt返回16位字符串）
            if (password != null && password.length() != 16) {
                // 对密码进行MD5加密
                String encryptedPassword = MD5Util.encrypt(password);
                if (encryptedPassword != null) {
                    user.setPassword(encryptedPassword);
                    usersToUpdate.add(user);
                    encryptedCount++;
                }
            }
        }
        
        // 批量更新用户密码
        if (!usersToUpdate.isEmpty()) {
            this.updateBatchById(usersToUpdate);
        }
        
        return encryptedCount;
    }

}
